//
//  LCRecordCounter.m
//  LuckyCam
//
//  Created by lsq on 2019/5/24.
//  Copyright © 2019 LuckyCam. All rights reserved.
//

#import "LCRecordCounter.h"
#import "Masonry.h"

@interface LCRecordCounter(){
    int   hh;
    int   mm;
    int   ss;

}

@property (nonatomic, strong) UIImageView *recordImageView;

@property (nonatomic, strong) UILabel *recordTimeLabel;

@property (nonatomic, strong) NSTimer *recordingTimer;


@end


@implementation LCRecordCounter

- (instancetype)init{
    if (self = [super init]) {
        [self initUI];
    }
    return self;
    
}

- (void)initUI{
    self.recordImageView = [UIImageView new];
    [self addSubview:self.recordImageView];
    [self.recordImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(10, 10));
        make.left.equalTo(@0);
    }];
//    self.recordImageView.image = [UIImage imageNamed:@"录制图标"];
    self.recordImageView.backgroundColor = [UIColor redColor];
    self.recordImageView.layer.cornerRadius = 5;

    
    self.recordTimeLabel = [UILabel new];
    [self addSubview:self.recordTimeLabel];
    [self.recordTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.recordImageView.mas_right).offset(10);
        make.top.bottom.equalTo(@0);
        make.right.equalTo(@0);
    }];
    self.recordTimeLabel.textAlignment = NSTextAlignmentLeft;
    self.recordTimeLabel.textColor = [UIColor colorWithRed:194/255. green:194/255. blue:194/255. alpha:1];
    self.recordTimeLabel.font = [UIFont systemFontOfSize:17];
    self.recordTimeLabel.text = @"00:00:00";
    
    _recordingTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                       target:self
                                                     selector:@selector(recordingTimerHandler)
                                                     userInfo:nil
                                                      repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:_recordingTimer forMode:NSRunLoopCommonModes];
    [_recordingTimer setFireDate:[NSDate distantFuture]];

}

- (void)startRecord:(BOOL)isStart time:(int)time cycleRecordTime:(int)cycletime{
    // 不在录像中
    if (!isStart) {
        return;
    }
    self.recordTime = cycletime;
    ss = time % 60;
    mm = (time / 60) % 60;
    hh = time / 3600;
    self.recordTimeLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d",hh, mm, ss];
    [_recordingTimer setFireDate:[NSDate distantPast]];
}

- (void)stopTimer{
    
    [_recordingTimer setFireDate:[NSDate distantFuture]];
    ss = 0;
    mm = 0;
    hh = 0;
    self.recordTimeLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d",hh, mm, ss];

}

// 重新计时
- (void)reCount{
    ss = 0;
    mm = 0;
    hh = 0;

}

- (void)recordingTimerHandler{
    self.recordImageView.hidden = !self.recordImageView.hidden;
    if (self.secondLessBlock) {
        self.secondLessBlock();
    }
    if (self.recordTime){
        int totalTime = ss + mm * 60 + hh * 60 * 60;
        if (totalTime >= self.recordTime) {
            ss=0;
            mm=0;
            hh=0;
        }
    }
    ss++;
    if (ss == 60){
        mm++;
        ss = 0;
    }
    if (mm == 60)
    {
        hh++;
        mm = 0;
    }
    
    self.recordTimeLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d",hh, mm, ss];
}

@end
